using System.Collections;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    public static GameManager Instance { get; private set; }
    private int birdCount = 0;
    private int pigCount = 0;
    private int killCount = 0;

    [SerializeField] GameData gameData;
    [SerializeField] GameObject menu;
    [SerializeField] GameObject results;
    [SerializeField] GameObject failed;
    [SerializeField] GameObject star1;
    [SerializeField] GameObject star2;
    [SerializeField] GameObject star3;
    [SerializeField] GameObject pause;
    
    public bool birdFlying = false;

    void Awake()
    {
        Instance = this;
    }

    void Start()
    {
        Time.timeScale = 1;
        var game = Resources.Load<GameObject>($"Levels/Map{this.gameData.currentMapID}/Level{this.gameData.currentLevelID}");
        Instantiate(game);
        this.birdCount = FindObjectsByType<Bird>(FindObjectsSortMode.None).Count();
        this.pigCount = FindObjectsByType<Pig>(FindObjectsSortMode.None).Count();
    }

    void Update()
    {
        SystemSetting.Quit();
        SystemSetting.Restart();
    }

    public void UpdateBirdCount()
    {
        this.birdCount--;
        this.Results();
    }

    public void UpdatePigCount()
    {
        this.killCount++;
        this.Results();
    }

    private void Results()
    {
        if (this.birdCount <= 0 
            || this.killCount >= this.pigCount)
        {
            switch (this.killCount)
            {
                case 0:
                    // 失败
                    this.failed.SetActive(true);
                    break;
                case 1:
                    this.star1.SetActive(true);
                    break;
                case 2:
                    this.star1.SetActive(true);
                    this.star2.SetActive(true);
                    break;
                case 3:
                    this.star1.SetActive(true);
                    this.star2.SetActive(true);
                    this.star3.SetActive(true);
                    break;
            }

            this.results.SetActive(true);
            this.UpdateData();
        }
    }
    
    private void UpdateData()
    {
        var levels = this.gameData.mapData[this.gameData.currentMapID].levelData;
        var levelData = levels[this.gameData.currentLevelID];
        if (this.killCount > levelData.starCount)
        {
            levelData.starCount = this.killCount;
        }
        var nextLevelID = this.gameData.currentLevelID + 1;
        if (nextLevelID < levels.Length)
        {
            var nextLevelData = levels[nextLevelID];
            if (nextLevelData.starCount == -1 
                && nextLevelData.unlockCondition <= levelData.starCount)
            {
                // 解锁下一个关卡
                nextLevelData.starCount = 0;
            }
        }
        else
        {
            var nextMapID = this.gameData.currentMapID + 1;
            if(nextMapID < this.gameData.mapData.Length)
            {
                var nextMapData = this.gameData.mapData[nextMapID];
                if(nextMapData.starCount == -1)
                {
                    // 解锁下一个地图
                    nextMapData.starCount = 0;
                }
            }
        }
        
    }

    public void FlyFinished(float delayTime = .0f)
    {
        if (delayTime <= 0)
        {
            this.FlyFinishedCore();
        }
        else
        {
            StartCoroutine(FlyFinishedCoroutine(delayTime));
        }
    }

    IEnumerator FlyFinishedCoroutine(float delayTime)
    {
        yield return new WaitForSeconds(delayTime);
        FlyFinishedCore();
    }

    private void FlyFinishedCore()
    {
        this.birdFlying = false;
        Common.ResetCamera();
        this.UpdateBirdCount();
    }

    #region Menu
    public void Pauae()
    {
        Time.timeScale = 0;
        this.menu.SetActive(true);
        this.pause.SetActive(false);
    }

    public void Continue()
    {
        Time.timeScale = 1;
        this.menu.SetActive(false);
        this.pause.SetActive(true);
    }

    public void Restart()
    {
        SceneManager.LoadScene(Defines.GameScene);
    }

    public void Back()
    {
        this.gameData.fromGame = true;
        SceneManager.LoadScene(Defines.LevelScene);
    }
    #endregion
}
